var environment=_gl.environment||"production";
var ajaxUrl={
    "local":{
        "settimeoutTime":1400,
        "menuList":_gl._host_res+"/res/js/data/menuList.json"
    },
    "production":{
        "settimeoutTime":0,
        "menuList" : _gl._host_res + "/js/data/menuList.json"
    }
};

var _menuArr=[{
    "id": 1,
    "text": "系统配置",
    "isShow": false,
    "children": [{
        "id": 11,
        "text": "会员信息",
        "isShow": false,
        "isCurrentMenu": false,
        "attributes": {
            "href": "/views/sys/user.html"
        }
    }, {
        "id": 12,
        "text": "文件管理",
        "isShow": false,
        "isCurrentMenu": false,
        "attributes": {
            "href": "#"
        }
    }]
}, {
    "id": 2,
    "text": "课程管理",
    "isShow": true,
    "children": [{
        "id": 22,
        "text": "课程列表",
        "isShow": false,
        "isCurrentMenu": true,
        "attributes": {
            "href": "/views/course/list.html"
        }
    }]
}, {
    "id": 3,
    "text": "订单详情",
    "isShow": false,
    "children": [{
        "id": 31,
        "text": "订单信息",
        "isShow": false,
        "isCurrentMenu": false,
        "attributes": {
            "href": "#"
        }
    }, {
        "id": 32,
        "text": "支付流水",
        "isShow": false,
        "isCurrentMenu": false,
        "attributes": {
            "href": "#"
        }
    }]
}, {
    "id": 4,
    "text": "微信",
    "isShow": false,
    "children": [{
        "id": 41,
        "text": "粉丝列表",
        "isShow": false,
        "isCurrentMenu": false,
        "attributes": {
            "href": "/views/wx/fans.html"
        }
    },{
        "id": 42,
        "text": "分享记录",
        "isShow": false,
        "isCurrentMenu": false,
        "attributes": {
            "href": "/views/wx/share.html"
        }
    },{
        "id": 43,
        "text": "研值",
        "isShow": false,
        "isCurrentMenu": false,
        "attributes": {
            "href": "#"
        }
    }]
}];

//当前href
var current = {
    "href":"",
    "text":"",
    "hasCurrentMenu":false
}
$(function(){
    //设置高度
    $(".content-iframe").height($(window).height()-140);
    $(window).resize(_helper.throttle_duration(function(){
        $(".content-iframe").height($(window).height()-140);
    },400,800));

    //初始化左侧菜单栏
    var menuObj = {};
    var html='<div class="aside-item-list">';
    html+=createAsideItem(_menuArr);
    html+='</div>';

    $(".aside-loading").hide();
    $("#aside-block").append(html);

    if(current.href){
        addNewIframe();
    }
    //setTimeout(function(){
    //    $.ajax({
    //        type: "POST",
    //        url:ajaxUrl[environment].menuList,
    //        data: menuObj,
    //        dataType : "json"
    //    }).done(function(data) {
    //        if(data && data.code==200){
    //            var html='<div class="aside-item-list">';
    //            html+=createAsideItem(data.data);
    //            html+='</div>';
    //
    //            $(".aside-loading").hide();
    //            $("#aside-block").append(html);
    //
    //            if(current.href){
    //                addNewIframe();
    //            }
    //        }else{
    //
    //        }
    //    }).fail(function(){
    //    }).always(function(){
    //    });
    //},ajaxUrl[environment].settimeoutTime);

    function createAsideItem(arr){
        var html='';
        var ihtml='';
        var cla_1='';
        var cla_2='';
        var href="";
        var hasChildren = false;
        if(arr && arr.length>0){
            for(var a= 0,a1=arr.length;a<a1;a++){
                href = (arr[a].attributes&&arr[a].attributes.href) || "#";
                if(arr[a].children && arr[a].children.length>0){
                    hasChildren = true;
                    ihtml="";
                }else{
                    hasChildren = false;
                    ihtml='<i class="fa fa-dot-circle-o" aria-hidden="true"></i>';
                }

                if(arr[a].isCurrentMenu && !current.hasCurrentMenu){
                    current.href = href;
                    current.text = arr[a].text;
                    cla_2=" current";
                    current.hasCurrentMenu=true;
                }else{
                    cla_2="";
                }
                html+='<div class="aside-item"><span class="'+cla_2+'" data-href="'+href+'">'+ihtml+arr[a].text+'</span>';

                if(hasChildren){
                    if(arr[a].isShow){
                        html+='<i class="fa-icon fa fa-minus-square" aria-hidden="true"></i>';
                        cla_1=' isShow';
                    }else{
                        html+='<i class="fa-icon fa fa-plus-square" aria-hidden="true"></i>';
                        cla_1="";
                    }

                    html+='<div class="aside-item-list-sub'+cla_1+'">';
                    html+=arguments.callee(arr[a].children);
                    html+='</div>';
                }
                html+='</div>';
            }
        }
        return html;
    }
    //左侧菜单栏的事件
    $(document).on("click",".aside-item",function(e){
        e.stopPropagation();
        var $this = $(this);
        var $thisA = $this.children("span");
        var $thisAHref=$thisA.data("href");
        if($thisAHref!="#" && $thisAHref!="javascript:;"){
            if($thisAHref == current.href) return false;
            current.href = $thisAHref;
            current.text = $thisA.text();
            $(".aside-block").find(".current").removeClass("current");
            $thisA.addClass("current");
            addNewIframe();
        }else{
            var $sub = $this.children(".aside-item-list-sub");
            if($sub.length>0){
                var $siblings = $this.siblings(".aside-item");
                $siblings.children(".fa-icon").removeClass("fa-minus-square").addClass("fa-plus-square");
                $siblings.children(".aside-item-list-sub").removeClass("isShow");
                $this.children(".aside-item-list-sub").toggleClass("isShow");
                var $thisI = $this.children(".fa-icon");
                if($thisI.hasClass("fa-plus-square")){
                    $thisI.removeClass("fa-plus-square").addClass("fa-minus-square");
                }else{
                    $thisI.removeClass("fa-minus-square").addClass("fa-plus-square");
                }
            }

        }
    });

    //刷新iframe
    $(".btn-refresh").on("click",function(){
        addNewIframe();
    });

    //登出
    $(".btn-logout").on("click",function(e){
        e.stopPropagation();
        _server.UserInfo_Service().setLoginState("logout");
        location.href="login.html";
    });

})

//点击菜单
function addNewIframe(obj){
    if(obj) current = obj;
    showIframeLoading();
    $(".content-name").find(".text").html(current.text)
    setTimeout(function(){
        $(".content-iframe").append(createFrame());
        isLoadFrame();
    },800);
}
//显示iframe的加载动画
function showIframeLoading(){
    if($(".content-iframe>.friday-loading").length<=0){
        $(".content-iframe").append('<div class="friday-loading"></div>');
    }
    $(".content-iframe").find("iframe").remove();
    $(".content-iframe>.friday-loading").show();
}
function hideIframeLoading(){
    $(".content-iframe>.friday-loading").hide();
}

//创建新的iframe
function createFrame(url) {
    if(!url) url = current.href;
    var s = '<iframe id="mainFrame" name="mainFrame" scrolling="auto" frameborder="no" border="0" marginwidth="0" marginheight="0" allowtransparency="yes" src="'
        + (_gl._host_res+url) + '" style="width:100%;height:100%;"></iframe>';
    return s;
}
//判断iframe是否加载成功
function isLoadFrame(url,callback){
    if(!url) url = current.href;
    var jq = top.jQuery;
    var $iframe = jq('iframe[src="'+url+'"]');
    if (isIE()) {
        //IE浏览器
        $iframe[0].onload = function () {
            hideIframeLoading();
            if(callback) callback();
        }
    } else {
        $iframe.contents().ready(function () {
            hideIframeLoading();
            if(callback) callback();
        });
    }
}
//判断是否是IE浏览器
function isIE(){
    var userAgent = window.navigator.userAgent.toLowerCase();
    if (window.navigator.appName == "Microsoft Internet Explorer") {
        try {
            var version = userAgent.match(/msie ([\d.]+)/)[1];
            if (isNaN(version) == false)
                return true;
        } catch (e) {
            return false;
        }
    }
    return false;
}